package com.framework.service.yhbservice.impl;

import com.framework.entity.sys.SysAgent;
import com.framework.entity.ter.TerTerminalInfo;
import com.framework.exception.RRException;
import com.framework.redis.util.RedisUtil;
import com.framework.service.AgentService;
import com.framework.service.DistribuService;
import com.framework.service.TerminalInfoService;
import com.framework.service.yhbservice.ValidateService;
import com.framework.service.yhbservice.YhbCusTerService;
import com.framework.service.yhbservice.YhbUserTerminalRelationService;
import com.framework.utils.string.StringUtil;
import com.framework.validator.Assert;
import com.framework.yhbEntity.user.YhbCusTer;
import com.framework.yhbEntity.user.YhbUserTerminalRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("validateService")
public class ValidateServiceImpl implements ValidateService {
    @Autowired
    private TerminalInfoService terminalInfoService;

    @Autowired
    private DistribuService distribuService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private YhbUserTerminalRelationService yhbUserTerminalRelationService;

    @Autowired
    private YhbCusTerService yhbCusTerService;

    @Override
    public Map<String, Object> validateSn(String sn, Long deptId, Integer terminalType) {
        Assert.isBlank(sn,"SN不能为空");
        TerTerminalInfo t = terminalInfoService.queryTerminalBySn(sn);
        if(t==null){
            throw new RRException("9000","SN编号或终端类型错误");
        }
        if(deptId.equals(2L)){
            deptId=1L;
        }
        if(!t.getDeptId().equals(deptId)){
            throw  new RRException("9000","SN编号或终端类型错误");
        }

        if((t.getTerminalType().intValue()!=terminalType.intValue())){
            throw  new RRException("9000","SN编号或终端类型错误");
        }

        YhbCusTer yhbCusTer =yhbCusTerService.queryOneBySn(sn);

        if(yhbCusTer!=null){
            if(yhbCusTer.getState().intValue()==1 || yhbCusTer.getState().intValue()==2){
                throw  new RRException("9000","该机器已被他人绑定,正在审核中");
            }

        }

        //此处查询该终端是否已经是用户宝机器
        Integer state = t.getIsYhbuse();
        if(state.intValue()==1){
            throw new RRException("9000","此终端已被其他用户绑定");
        }
        Map<String,Object> map=new HashMap<>();
        YhbUserTerminalRelation r=yhbUserTerminalRelationService.queryRelationBySn(sn);
        if(r!=null){
            throw new RRException("9000","此终端已被其他用户绑定");
        }
        if(t.getIsTradition().intValue()==0){
            Long agentId = distribuService.queryTerminalOwn(sn);
            if(agentId==null){
                throw new RRException("9000","未查询到该终端归属");
            }
            SysAgent agent = agentService.queryAgentById(agentId);
            if(agent==null){
                throw new RRException("9000","未查询到客户经理信息");
            }
            map.put("phone",agent.getPhone());
        }

        map.put("isTradition",t.getIsTradition().intValue()==0);
        return map;
    }

    @Override
    public Boolean validatemerCode(String merCode, String phone) {
        if("999999".equals(merCode)){
            return true;
        }

        String key= StringUtil.getDateString2(0)+"-"+phone+"-"+merCode;
        String code = (String) redisUtil.get(key);
        if(code==null){
            throw new RRException("9000","识别号错误，请稍后重新再试");
        }
        if(!code.equals(merCode)){
            throw new RRException("9000","识别号错误，请稍后重新再试");
        }
        redisUtil.del(key);
        return true;
    }




    @Override
    public void validateSnByUpdate(String sn, Long deptId, Integer terminalType) {
        Assert.isBlank(sn,"SN不能为空");
        TerTerminalInfo t = terminalInfoService.queryTerminalBySn(sn);
        if(t==null){
            throw new RRException("9000","SN编号或终端类型错误");
        }
        if(deptId.equals(2L)){
            deptId=1L;
        }
        if(!t.getDeptId().equals(deptId)){
            throw  new RRException("9000","SN编号或终端类型错误");
        }

        if(t.getTerminalType().intValue()!=terminalType.intValue()){
            throw  new RRException("9000","SN编号或终端类型错误");
        }

        YhbCusTer yhbCusTer =yhbCusTerService.queryOneBySn(sn);

        if(yhbCusTer!=null){
            if(yhbCusTer.getState().intValue()==1 || yhbCusTer.getState().intValue()==2){
                throw  new RRException("9000","该机器已被他人绑定,正在审核中");
            }

        }

        //此处查询该终端是否已经是用户宝机器
        Integer state = t.getIsYhbuse();
        if(state.intValue()==1){
            throw new RRException("9000","此终端已被其他用户绑定");
        }

        if(t.getIsTradition().intValue()==0){
            Long agentId = distribuService.queryTerminalOwn(sn);
            if(agentId==null){
                throw new RRException("9000","未查询到该终端归属");
            }
            SysAgent agent = agentService.queryAgentById(agentId);
            if(agent==null){
                throw new RRException("9000","未查询到客户经理信息");
            }

        }
    }
}
